//
//  AppDelegate.m
//  catClaw
//
//  Created by iOS Developer 1 on 16/3/11.
//  Copyright © 2016年 Joey. All rights reserved.
//

#import "AppDelegate.h"
#import "HTTPServer.h"
#import "ViewController.h"
#import "CCHTTPConnection.h"
#import <AVFoundation/AVFoundation.h>

@interface AppDelegate ()

{
    AVAudioPlayer *_audioPlayer;
}
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [self setLog];
    [self playBackground];
    [self initHttpServer];
    
    return YES;
}

- (void)setLog
{
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
    [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor greenColor] backgroundColor:[UIColor clearColor] forFlag:LOG_FLAG_INFO];
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor yellowColor] backgroundColor:[UIColor clearColor] forFlag:LOG_FLAG_WARN];
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor redColor] backgroundColor:[UIColor clearColor] forFlag:LOG_FLAG_ERROR];
}
/**
 *  创建Server
 */
- (void)initHttpServer
{
    
    self.httpserver = [[HTTPServer alloc] init];
    [self.httpserver setType:@"_http._tcp."];
    [self.httpserver setPort:5432];
    
    /**
     *  webPath is filesystem root path
     */

     NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"website"];
   // NSString *webPath = @"http://iosdev2.local:8000/";
   // NSString *webPath = @"http://192.168.2.111:8080/feimaorexchange/";
    [self.httpserver setDocumentRoot:webPath];
    [self.httpserver setConnectionClass:[CCHTTPConnection class]];
    [self startServer];
}

- (void) startServer
{
    NSError *error;
    if ([self.httpserver start:&error]){
        DDLogInfo(@"start!!");
    }else{
         DDLogError(@"Error Started HTTP Server:%@", error);
    }
}


//播放背景音乐让服务器有后台能力
- (void)playBackground {
    
    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:kAudioSessionProperty_OverrideCategoryMixWithOthers error: &setCategoryErr];
    [[AVAudioSession sharedInstance] setActive:YES error: &activationErr];
    
    NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"mp3"];
    NSURL *url = [NSURL fileURLWithPath:musicPath];
    
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [_audioPlayer prepareToPlay];
    [_audioPlayer setVolume:1];
    _audioPlayer.numberOfLoops = -1; //设置音乐播放次数  -1为一直循环
    [_audioPlayer play];
}


@end
